<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{padding: 0;margin: 0;}
        .box{
            width: 200px;
            height: 200px;
            background-color: rgba(0,0,0,0.4);
            margin: 10px;
          
        }
        .sub{
            width: 100px;
            height: 100px;
            background-color: #6ff;
            margin: 50px;
            pointer-events: none;
        }
    </style>
</head>
<body>  
    <!-- box.onclick = function(){alert(1111)} -->
    <!-- <div class="box" onclick="alert(1111)"></div>
    <div class="box" onclick="say()"></div> -->

    <button class="remove">删除</button>
    <div class="box"></div>
</body>
<script>
    // 给一个元素绑定事件的方式
    // 1. 内联JS绑定事件  (了解)   box.removeAttribute("onclick")
    // 2. 普通绑定(DOM一级绑定  赋值式)        => box.onclick = null
    // 3. 事件监听(侦听)绑定事件   DOM二级绑定  =>  

    // 回调函数(把函数当成参数传到另一个函数中  相当于提前规定好了要执行的内容 )

    // 添加事件监听
    // ele.addEventListener(eventType,callback,bool)
    // ele         绑定事件元素
    // eventType   绑定事件的类型 (click dblclick )
    // callback    回调函数,事件出发时执行的内容
    // bool        (可选)

    // 删除事件监听  => 注意对应回调函数删除
    // ele.removeEventListener(eventType,callback,bool)
    // ele         绑定事件元素
    // eventType   绑定事件的类型 (click dblclick )
    // callback    回调函数,事件出发时执行的内容
    // bool        (可选)

    


    var box = document.getElementsByClassName("box")[0];
    var remove = document.getElementsByClassName("remove")[0];
    console.dir(box);
   
   /*  box.addEventListener("click",function(){
        console.log("开始随机颜色");
        this.style.background = randColor();
    });

    box.addEventListener("click",function(){
        console.log(111111);
    });


    remove.onclick = function(){
        box.removeEventListener("click",function(){
            console.log("开始随机颜色");
            this.style.background = randColor();
        });

        box.removeEventListener("click",function(){
            console.log(111111);
        });

    }
 */

    function clickHandler_1(){
        console.log("开始随机颜色");
        this.style.background = randColor();
    }

    var  clickHandler_2 = function(){
        console.log(111111);
    }

    box.addEventListener("click",clickHandler_1);

    box.addEventListener("click",clickHandler_2);

    remove.onclick = function(){
        box.removeEventListener("click",clickHandler_1);
        box.removeEventListener("click",clickHandler_2);
    }


 
   

    function randColor(){
        return `rgba(${randNum()},${randNum()},${randNum()},1)`
    }

    function randNum(){
        return Math.round(Math.random()*255);
    }


    function say(){
        alert("hello");
    }

   

</script>
</html>